import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDao {
    public User selectByName(String userName){

        Connection connection=null;
        PreparedStatement statement = null;
        ResultSet resultSet=null;

        try {
            String sql="select * from user where userName = ?";
            connection=DBUtil.getConnection();
            statement=connection.prepareStatement(sql);
            statement.setString(1,userName);
            resultSet=statement.executeQuery();
            if(resultSet.next()){
                User user=new User();
                user.setUserId(resultSet.getInt("userId"));
                user.setUserName(resultSet.getString("userName"));
                user.setPassword(resultSet.getString("password"));
                user.setContact(resultSet.getString("contact"));
                user.setBlogCount(resultSet.getInt("blogCount"));
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(resultSet,statement,connection);
        }
        return null;
    }
    public User selectById(int userId){

        Connection connection=null;
        PreparedStatement statement = null;
        ResultSet resultSet=null;

        try {
            String sql="select * from user where userId = ?";
            connection=DBUtil.getConnection();
            statement=connection.prepareStatement(sql);
            statement.setInt(1,userId);
            resultSet=statement.executeQuery();
            if(resultSet.next()){
                User user=new User();
                user.setUserId(resultSet.getInt("userId"));
                user.setUserName(resultSet.getString("userName"));
                user.setPassword(resultSet.getString("password"));
                user.setContact(resultSet.getString("contact"));
                user.setBlogCount(resultSet.getInt("blogCount"));
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(resultSet,statement,connection);
        }
        return null;
    }
    public void upDate(int n){
        Connection connection=null;
        PreparedStatement statement=null;
        String sql=null;
        try {
            if(n==-1){
                sql="update user set blogCount=blogCount-1";
            }else{
                sql="update user set blogCount=blogCount+1";
            }
            connection=DBUtil.getConnection();
            statement=connection.prepareStatement(sql);
            int ret=statement.executeUpdate();
            if(ret==1){
                System.out.println("删除成功");
            }else {
                System.out.println("删除失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(null,statement,connection);
        }
    }
}
